<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Apr 19, 2012
 * Time: 10:23:51 PM
 * To change this template use File | Settings | File Templates.
 */

class User {
    private $db;
    private $id;
    private $fullName;
    private $initName;
    private $userName;
    private $password;
    private $email;
    private $nic;
    private $gender;
    private $birthday;
    private $address_p = array();
    private $address_t = array();
    private $phone = array();
    private $status;
    private $isDelete;
    private $version;
    private $profile_id;

    function __set($name, $value) {
        // TODO: Implement __set() method.
        $this->$name = $value;

    }

    function __get($name) {
        // TODO: Implement __get() method.
        return $this->$name;
    }

    public function __construct() {
        $ado = new AdodbDriver();
        $this->db = $ado->pConnect(true);
        $this->db->debug = false;
    }

    public function getGrid($posStart, $count, $userName, $name, $nic, $sort_by, $sort_dir) {
        $sql = "SELECT  aea_user.id as ID,user_name,full_name,nic,email,profile_id,aea_user.status as status FROM aea_profile,aea_user Where aea_user.is_delete=0 AND aea_profile.id=aea_user.profile_id";
        if ($userName != '')
            $sql .= " and user_name like '$userName%'";
        if ($name != '')
            $sql .= " and full_name like '$name%'";
        if ($nic != '')
            $sql .= " and nic like '$nic%'";
        if ($sort_dir == '')
            $sort_dir = "asc";
        if ($sort_by != '')
            $sql .= " Order By $sort_by $sort_dir";
        if ($posStart == 0) {
            $sqlCount = "Select count(*) as cnt from ($sql) as tbl";

            $resCount = $this->db->Execute($sqlCount);
            if (!$resCount)
                print $this->db->ErrorMsg();
            else
                while (!$resCount->EOF) {
                    $totalCount = $resCount->fields[0];
                    $resCount->MoveNext();
                }

        }
        $sql .= " LIMIT " . $posStart . "," . $count;
        $res = $this->db->Execute($sql);
        if ($res) {
            $xmlString = "<?xml version=\"1.0\"  encoding=\"ISO-8859-1\"?><rows total_count='" . $totalCount . "' pos='" . $posStart . "'>";
            while (!$res->EOF) {
                $xmlString .= "<row id='" . $res->fields['ID'] . "'>
                    <cell>" . $res->fields['user_name'] . "</cell>
                    <cell>" . $res->fields['full_name'] . "</cell>
                    <cell>" . $res->fields['nic'] . "</cell>
                    <cell>" . $res->fields['email'] . "</cell>
                    <cell>" . $res->fields['profile_id'] . "</cell>
                    <cell>" . $res->fields['status'] . "</cell>
                    </row>";
                $posStart++;
                $res->MoveNext();
            }
            $xmlString .= "</rows>";
        } else {
            $xmlString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $xmlString;
    }

    public function save() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            ADODB_Active_Record::ClassHasMany('Profile', 'userid', 'profile_id', 'UserAR');
            $profile = new Profile();
            $profile->userid = new UserAR();
            if ($this->profile_id != '' AND $this->profile_id != NULL) {
                $profile->Load("id=$this->profile_id");
            } else {
                $profile->create_time = date('Y-m-d H:i:s', time());
                $profile->create_user_id = $_SESSION['user_id'];
            }
            $profile->full_name = $this->fullName;
            $profile->nic = $this->nic;
            $profile->email = $this->email;

            $profile->update_user_id = $_SESSION['user_id'];

            $profile->version = $this->version;
            $i = $profile->save();


            if ($this->id != '' AND $this->id != NULL) {
                $profile->userid->Load("id=$this->id");
            } else {
                $profile->userid->create_time = date('Y-m-d H:i:s', time());
                $profile->userid->create_user_id = $_SESSION['user_id'];
                $profile->userid->is_delete = 0;
            }
            //$profile->userid->id = $this->id;
            $profile->userid->user_name = $this->userName;
            if ($this->password != '' AND $this->password != NULL)
                $profile->userid->password = SHA1($this->password);

            $profile->userid->update_user_id = $_SESSION['user_id'];
            $profile->userid->profile_id = $profile->id;
            $profile->userid->version = $this->version;
            $profile->userid->status = $this->status;
            $j = $profile->userid->save();

        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function edit() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            $profile = new Profile();
            if ($this->profile_id != '' AND $this->profile_id != NULL) {
                $profile->Load("id=$this->profile_id");
            } else {
                return 0;
            }
            $profile->full_name = $this->fullName;
            $profile->init_name = $this->initName;
            $profile->nic = $this->nic;
            $profile->email = $this->email;
            $profile->gender = $this->gender;
            $profile->birthday = $this->birthday;
            $profile->theme = $this->themel;
            $profile->update_user_id = $_SESSION['user_id'];
            $i = $profile->save();
        } catch (exception $e) {
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function getProfile() {
        $sql = "SELECT  * FROM aea_profile Where id=$_SESSION[profile_id] LIMIT 1";
        $res = $this->db->Execute($sql);

        return $res;
    }

    public function validatePassword($id, $password) {
        $sql = "SELECT  * FROM aea_user Where id=$id AND password=SHA1('$password') LIMIT 1";
        $recordSet = $this->db->Execute($sql);
        return $recordSet->FetchRow();

    }

    public function resetPassword() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            if ($this->id != '' AND $this->id != NULL) {
                $user = new UserAR();
                $user->Load("id=$this->id");
                $user->password = SHA1($this->password);
                $i = $user->save();
            }
        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
            return print_r($e);
        }
        return $i;

    }

    public function actionGetThemeListMenu() {
        $sql = 'select theme_name,id from aea_theme';
        $themeMenuList = $this->db->Execute($sql);
        return $themeMenuList->GetMenu2('theme', '', '0:--selected--', '', '', 'id="theme"');
    }

    public function delete() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            ADODB_Active_Record::ClassHasMany('Profile', 'userid', 'profile_id', 'UserAR');
            $profile = new Profile();
            $profile->userid = new UserAR();
            if ($this->profile_id != '' AND $this->profile_id != NULL) {
                $profile->Load("id=$this->profile_id");
                $profile->update_user_id = $_SESSION['user_id'];
                $profile->is_delete = 1;
                $j = $profile->save();
            }
            if ($this->id != '' AND $this->id != NULL) {
                $profile->userid->Load("id=$this->id");
                $profile->userid->is_delete = 1;
                $profile->update_user_id = $_SESSION['user_id'];
                $i = $profile->userid->save();
            }
        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

}

require_once('libraries/adodb5/adodb-active-record.inc.php');
class UserAR extends ADOdb_Active_Record {
    var $_table = 'aea_user';

}

class Profile extends ADOdb_Active_Record {
    var $_table = 'aea_profile';

}

